package com.springboot.mongodb.impl;

import com.springboot.mongodb.dao.HtmDao;
import com.springboot.mongodb.dao.LabelDao;
import com.springboot.mongodb.model.HtmDTO;
import com.springboot.mongodb.model.HtmDepository;
import com.springboot.mongodb.model.HtmVO;
import com.springboot.mongodb.model.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @ClassName springboot_mongodb - HtmServiceImpl
 * @Description [此类用于: ]
 * @Author liming-jybdd@foxmail.com
 * @Date 2021/9/289:52
 * @Version 1.0
 **/
@Service
public class HtmServiceImpl {

    @Autowired
    private HtmDao htmDao;

    @Autowired
    private LabelDao labelDao;

    public List<HtmDepository> findAllHtmMode(){
        return htmDao.findAll();
    }
    public Optional<HtmDepository> findByIdHtmMode(HtmDepository htmDepository){
        return htmDao.findById(htmDepository.getId());
    }


    public void saveMode(HtmDTO htmDTO){
        HtmDepository htmDepository = new HtmDepository();
        htmDepository.setHtmlExcerpt(htmDTO.getHtmlExcerpt());
        htmDao.insert(htmDepository);
    }

    public HtmVO userHtmById(Label label) {
        label = labelDao.findByUserId(label.getUserId());
        HtmDepository htmDepository = new HtmDepository();
        htmDepository.setId(label.getHtmId());
        Optional<HtmDepository> htmDepository1 = htmDao.findById(htmDepository.getId());
        String name = htmDepository1.map(HtmDepository::getHtmlExcerpt).get();

        HtmVO htmVO = new HtmVO();
        htmVO.setHtmlExcerpt(name);
        htmVO.setBreadcrumbs(label.getBreadcrumbs());
        return htmVO;
    }

}